#! /usr/bin/env python
# -*- coding: utf-8 -*-
import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header


def postEmail(content,email,sender_email,sender_password):
    """
    email:邮箱地址
    content：要发送的内容
    """
    print("-------------postEmail--------------")
    print(email)
    print(content)
 
    # 发件人邮箱账号和授权码
    # sender_email = 'proud2008@126.com'
    # 通过126后台设置 POP3/SMTP/IMAP生成的
    # sender_password = 'HPaPWvrT7HTukX25'
    # sender_password = 'JINGtiger1122'
    # 收件人邮箱
    receiver_email = email
    # 邮件主题和内容
    subject = 'xotools'
    # 构建邮件对象
    message = MIMEText(content, 'plain', 'utf-8')
    message['From'] = Header(sender_email, 'utf-8')
    message['To'] = Header(receiver_email, 'utf-8')
    message['Subject'] = Header(subject, 'utf-8')
    re=False
    error_message=""
    # 连接 SMTP 服务器并发送邮件
    try:
        # 以 QQ 邮箱为例，SMTP 服务器地址和端口
        smtp_server = smtplib.SMTP_SSL('smtp.126.com', 465)
        smtp_server.login(sender_email, sender_password)
        smtp_server.sendmail(sender_email, receiver_email, message.as_string())
        print('邮件发送成功')
        re= True
    except Exception as e:
        print(f'邮件发送失败: {e}')
        error_message=str(e)
        
    finally:
        smtp_server.quit()
    return re,error_message



if __name__ == '__main__':
    t=postEmail("AA","404171231@qq.com","proud2008@qq.com","HPaPWvrT7HTukX25")
    print(t)